#
#  SimpleRegex.pm
#  CamelBonesExample
#
#  Created by Jim Turner on 12/30/05.
#  Copyright 2005 __MyCompanyName__. All rights reserved.
#

package SimpleRegex;

use CamelBones qw(:All);

use strict;
use warnings;

class SimpleRegex {
	'super' => 'NSObject',
	'properties' => [ ],
};

sub init : Selector(init) ReturnType(@)
{
	my( $self ) = @_;
	
	$self = $self->SUPER::init();

	return( $self );
}

sub parseRegex_onText : Selector(p_parseRegex:onText:) ReturnType(@) ArgTypes(@@)
{
	my( $self, $evalString, $text ) = @_;

	# We can't do $text =~ $regex, it won't work.  So we build the segment of 
	# perl that does the regex evaluation in the calling Cocoa and pass it in its
	# entirety as $evalString.  eval() will simply do the business here as a self-contained
	# mini program.  You can even check $@ for errors if you like...
	eval( $evalString );
	
	# Because Cocoa is expecting a string in return, an empty string in perl would
	# cause an error... and it would leave the user not really knowing what happened anyways.
	# So kill two birds with one stone!
	if( $text eq "" )
	{
		$text = "Pattern Match Removed all Characters";
	}
	
	return( $text );
}

sub testRegex : Selector(p_testRegex:) ReturnType(@) ArgTypes(@)
{
	my( $self, $evalString ) = @_;

	# Check for affluent perl programmers
	if( $evalString =~ /\;$/ )
	{
		return( "syntax error: Found semicolon at end of pattern" );
	}

	my $testString = "This is just a test string... it could be anything, really";
	
	eval( $evalString );

	# Return everything eval found as an error... it might be a lot,
	# it might be nothing at all.  Can't combine the chomp and return...
	# chomp returns the number of characters chomped and the calling Cocoa
	# is expecting a string.
	chomp( $@ );
	return( $@ );
}

1;
